/*
 * Copyright 2008 - 2009 Lars Heuer (heuer[at]semagia.com). All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.semagia.atomico.server.impl.jaxrs;

import java.util.Date;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

import com.semagia.atomico.server.IConfiguration;
import com.semagia.atomico.server.IServerApplication;
import com.semagia.atomico.server.ServerApplicationProvider;
import com.semagia.atomico.server.storage.IModifiableStorage;
import com.semagia.atomico.server.storage.IStorage;

/**
 * Abstract base class for all resources.
 * 
 * Provides mainly access to the underlying storage and configuration.
 * 
 * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a>
 */
abstract class AbstractBaseResource {

    protected final Request _request;

    private final IServerApplication _app;

    AbstractBaseResource(final Request request) {
        _request = request;
        _app = ServerApplicationProvider.getServerApplication();
        if (_app == null) {
            throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * Returns the time when the resource was modified.
     * <p>
     * This method is invoked by {@link #makeResponseBuilder()} and this property
     * may be state-dependent.
     * </p>
     *
     * @return The time when this resource was modified.
     */
    protected abstract long lastModification();

    /**
     * Creates a {@link ResponseBuilder} with a last-modified header.
     * 
     * If the request contains a <tt>If-Modified-Since</tt> header and the
     * resource wasn't modified, a {@link WebApplicationException} with
     * the status <tt>Not modified (304)</tt> is thrown.
     *
     * @return A response builder.
     * @throws WebApplicationException In case the resource wasn't modified.
     */
    protected ResponseBuilder makeResponseBuilder() throws WebApplicationException {
        final Date lastModification = new Date(lastModification());
        ResponseBuilder builder = _request.evaluatePreconditions(lastModification);
        if (builder != null) {
            // Preconditions are met, report the status to the client
            throw new WebApplicationException(builder.build());
        }
        builder = Response.ok();
        builder.lastModified(lastModification);
        return builder;
    }

    /**
     * Returns the configuration instance.
     *
     * @return The configuration.
     */
    protected IConfiguration getConfiguration() {
        return _app.getConfiguration();
    }

    /**
     * Returns the underlying storage.
     *
     * @return The storage instance to operate upon, never <tt>null</tt>.
     */
    protected IStorage getStorage() {
        return _app.getStorage();
    }

    /**
     * Returns either an {@link IModifiableStorage}
     *
     * @return A {@link IModifiableStorage}.
     * @throws WebApplicationException If the storage is not modifiable.
     */
    protected IModifiableStorage getModifiableStorage() throws WebApplicationException {
        final IStorage storage = getStorage();
        if (!storage.isModifiable()) {
            throw new WebApplicationException(ResponseUtils.methodNotAllowed());
        }
        return (IModifiableStorage) storage;
    }

}
